package com.service.impl;

import java.util.List;   

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.MasterDao;
import com.entity.Master;
import com.github.pagehelper.PageHelper;
import com.service.MasterService;
import com.util.MD5Util;

@Service
@Transactional
public class MasterServiceImpl implements MasterService {
	@Autowired
	private MasterDao md;

	@Override
	// 登录
	public Master masterLogin(String sname, String password) {
		return md.masterLogin(sname, password);
	}

	@Override
	// 修改密码
	public boolean updateMasterPassword(String sname, String password) {
		return md.updateMasterPassword(sname, password);
	}

	@Override
	// 查询个人
	public Master selectMasterByMname(String mname) {
		return md.selectMasterByMname(mname);
	
	}

	
	
	@Override
	public boolean findMasterByMname(String sname) {
		Master findMasterByMname = md.findMasterByMname(sname);
		if (findMasterByMname != null) {
			return true;
		}
		return false;
	}

	@Override
	public Master isexistMaster(Integer mid) {
		return md.isexistMaster(mid);
	}
	
	// 根据手机号判断舍管是否存
	@Override
	public boolean findMasterByPhone(String phone) {
		Master findMasterByPhone = md.findMasterByPhone(phone);
		if (findMasterByPhone != null) {
			return true;
		}
		return false;
	}

	// 添加舍管
	@Override
	public boolean addMaster(Master master) {
		if (findMasterByPhone(master.getPhone())) {
			return false;
		}else {
			master.setMname(master.getMname());
			master.setPassword(master.getPassword());
			master.setSex(master.getSex());
			master.setPhone(master.getPhone());
			master.setSchool(master.getSchool());
			master.setDormitory(master.getDormitory());
			md.addMaster(master);
			return true;
		}

	}

	// 删除舍管
	@Override
	public boolean deleteMasterById(Integer mid) {
		return md.deleteMasterById(mid);
	}

	// 查询全部舍管
	@Override
	public List<Master> findAllMasters(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Master> findAllMasters = md.findAllMasters();
		return findAllMasters;
	}

	// 根据宿舍楼查询舍管
	@Override
	public List<Master> findMastersByDormitory(String dormitory, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Master> findMastersByDormitory = md.findMastersByDormitory(dormitory);
		return findMastersByDormitory;
	}

	@Override
	public boolean updateMasterInfo(Master master) {
		return md.updateMasterInfo(master);
	}

	@Override
	//修改个人信息
	public boolean updateMaster(Master master) {
		return md.updateMaster(master);
	}

}
